<?php
/**
 * 新闻动态
 * Created by PhpStorm.
 * User: xsh
 * Date: 2018/4/12  10:06
 */
namespace app\mobile\controller;
use think\Controller;

class Center extends Base
{
    /*
    *	构造函数
    */
    function __construct(){
        parent::__construct();
        //if(empty($this->info_user))
    }

    public function zindex()
    {
        $uid = $this->info_user['id'];
        $username = $this->info_user['username'];

        $admin_role_id = db('admin')->where(['id'=>$uid])->field('role_id')->find();
        $member_authority = db('member_authority')->where(['id'=>$admin_role_id['role_id']])->field('name')->find();

        //下载数量
        $down_record = db('down_record')->where(['uid'=>$uid])->count();

        //企业关注数
        $attention = db('attention')->where(['uid'=>$uid])->count();

        $this->assign('username',$username);
        $this->assign('type_name',$member_authority['name']);
        $this->assign('attention',$attention);
        $this->assign('down_record',$down_record);
        return $this->fetch();
    }

    //下载记录
    public function down_recording(){
        $uid = $this->info_user['id'];
        $down_record = db('down_record')->alias('dr')->join('file f','f.id=dr.fid')->where(['dr.uid'=>$uid])->order('dr.id desc')->select();

        $this->assign('down_record',$down_record);
        return $this->fetch();
    }

    //我的关注
    public function care()
    {
        $uid = $this->info_user['id'];

        if(request()->isAjax()){
            $page = input("page");

            $start_page = ($page - 1)*$this->per_page;

            $attention_result = db('attention')->alias('a')->join('xls_company xc','xc.id = a.company_id and a.status=1')->where(array('a.uid'=>$uid))->field('a.id as aid,a.status,xc.*')->order('a.id desc')->limit("$start_page,$this->per_page")->select();

            $attention_result = $this->zquery_handle($attention_result);
            $this->success($attention_result);
        }

        $attention_result = db('attention')->alias('a')->join('xls_company xc','xc.id = a.company_id and a.status=1')->where(array('a.uid'=>$uid))->field('a.id as aid,a.status,xc.*')->order('a.id desc')->limit($this->per_page)->select();

        $xls_company_count = db('attention')->alias('a')->join('xls_company xc','xc.id = a.company_id and a.status=1')->where(array('a.uid'=>$uid))->field('a.id as aid,a.status,xc.*')->order('a.id desc')->limit($this->per_page)->count();

        if($xls_company_count > $this->per_page){
            $click_for_more=true;
        }else{
            $click_for_more=false;
        }

        $attention_result = $this->zquery_handle($attention_result);

        $this->assign('attention_result',$attention_result);
        $this->assign('click_for_more',$click_for_more);
        return $this->fetch();
    }

    public function zquery_handle($array){
        if(!empty($array)){
            foreach ($array as $key=>&$value) {
                $are_id = [$value['province'],$value['city'],$value['area']];
                $where['id'] = array('in',$are_id);
                $area_result = db('area')->where($where)->field('name')->order('id asc')->select();
                $value['province'] = isset($area_result[0]['name'])?$area_result[0]['name']:'';
                $value['city'] = isset($area_result[1]['name'])?$area_result[1]['name']:'';
                $value['area'] = isset($area_result[2]['name'])?$area_result[2]['name']:'';
                $value['path_tem'] = $value['province'].$value['city'].$value['area'];

                $value['url'] = url('Company/zquery_result',['id'=>$value['id']]);

                if(!empty($this->info_user['id'])){
                    $attention = db('attention')->where(array('uid'=>$this->info_user['id'],'company_id'=>$value['id']))->find();
                    if(!empty($attention)){
                        $value['is_attention'] = $attention['status'];
                    }else{
                        $value['is_attention'] = 0;
                    } 
                }else{
                    $value['is_attention'] = 0;
                }
            }
        }

        return $array;
    }

    //账号资料
    public function member_data(){

        return $this->fetch();
    }

    //联系我们
    public function contact_us(){
        $web_set_result = db('web_set')->field('telephone,qr')->find();
        $this->assign('domain', $_SERVER['HTTP_HOST']);
        $this->assign('contact_us', $web_set_result);
        return $this->fetch();
    }

    public function headimage_edit(){
        if(request()->isAjax()){
            $headimage = request()->file('headimage');
            if(!empty($headimage)){
                $info = $headimage->move(ROOT_PATH . 'public/static/uploads');
                if ($info) {
                    $data['headimage'] = $info->getSaveName();
                }
            }
            $bool = db('member')->where(array('id'=>session('info_user')['id']))->update($data);

            if(!$bool)$this->error('修改失败');
            $this->success('修改成功!');
        }
    }

    public function edit(){
        if(request()->isAjax()){
            $data['username'] = input('username');
            $data['mobile'] = input('mobile');
            $data['company'] = input('company');
            $passwd =input('passwd');
            $data['sex'] = input('sex');
            $member = db('member')->where('id', session('info_user')['id'])->find();
            if($passwd != $member['passwd']){
                $data['passwd'] = md5($passwd);
            }else{
                $data['passwd'] = $passwd;
            }
            $bool = db('member')->where('id', session('info_user')['id'])->update($data);

            if($bool){
                $this->success('修改成功!',url('center/member_data'));
            }else{
                $this->error('修改失败',url('center/member_data'));
            }
        }
    }

    public function ajax_right(){
        $id = input('id');
        $user_find = db('member')->where('id', $id)->field('num,username')->find();
        $num = unserialize($user_find['num']);
        //    halt($num);

        $this->assign('num', $num);
        $this->assign('user_find',$user_find);
        return $this->fetch();
    }

}